﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace QS.Input
{
    public static class QMouseOperations
    {
        private static MouseState m_CurrentMouseState;
        private static MouseState m_LastMouseState;

        public static void UpdateMouseState(GraphicsDevice graphicsDevice)
        {      
            m_CurrentMouseState = Mouse.GetState();
            CurrentPosition = new Point(m_CurrentMouseState.X, m_CurrentMouseState.Y);

            int mouseX = (CurrentPosition.X - LastPosition.X);
            int mouseY = (CurrentPosition.Y - LastPosition.Y);

            PositionDelta = new Point(mouseX, mouseY);

            if (ResetMouse)
            {
                LastPosition = new Point(graphicsDevice.Viewport.X + graphicsDevice.Viewport.Width / 2, graphicsDevice.Viewport.Y + graphicsDevice.Viewport.Height / 2);
                Mouse.SetPosition(LastPosition.X, LastPosition.Y);
            }
            else
                LastPosition = CurrentPosition;

            LeftDown = m_CurrentMouseState.LeftButton == ButtonState.Pressed;
            MiddleDown = m_CurrentMouseState.MiddleButton == ButtonState.Pressed;
            RightDown = m_CurrentMouseState.RightButton == ButtonState.Pressed;

            LeftClicked = (m_LastMouseState.LeftButton == ButtonState.Released && LeftDown);
            MiddleClicked = (m_LastMouseState.MiddleButton == ButtonState.Released && MiddleDown);
            RightClicked = (m_LastMouseState.RightButton == ButtonState.Released && RightDown);

            m_LastMouseState = m_CurrentMouseState;
        }

        public static Boolean LeftClicked { get; private set; }
        public static Boolean RightClicked { get; private set; }
        public static Boolean MiddleClicked { get; private set; }

        public static Point CurrentPosition { get; private set; }
        public static Point LastPosition { get; private set; }
        public static Point PositionDelta { get; private set; }

        public static Boolean ResetMouse { get; set; }

        public static MouseState MouseState
        {
            get
            {
                return m_CurrentMouseState;
            }
        }

        public static Boolean LeftDown { get; private set; }
        public static Boolean RightDown { get; private set; }
        public static Boolean MiddleDown { get; private set; }
    }
}
